use  "C:\Users\patri\Dropbox\My Projects\Spring 2023 (Selective Sync Conflict)\Analysis\ChinaDebt\ChinaLead.dta",  clear
xtset leadid2 year


**Time Set Data 
stset enddate2 , id(leadid2) failure(fail==1) origin(time eindate) enter(time startdate2)

**Controls
global cntl growth v2x_polyarchy lnrgdpnapc  ny_gdp_totl_rt_zs  max_civilwar2 lndebt 

*Without democracy
global cntl2 growth  lnrgdpnapc  ny_gdp_totl_rt_zs  max_civilwar2 lndebt 



****************************************************************
***Descriptive Stats
lab var lnchinadebt_gdp2 "log China Debt"
lab var totalproject2 "New China Projects"

lab var v2x_execorr "Executive Corruption"
lab var v2x_polyarchy "Polyarchy"
lab var lnrgdpe "Log GDP"
lab var growth "Growth"
lab var democracy "Democracy (Binary)"
lab var lnrgdpnapc "Log GDP per cap"
lab var ny_gdp_totl_rt_zs "Resource Rents"
lab var max_civilwar2 "Civil Conflict"
lab var lndebt "log Debt/GDP"

lab var lnms_mil_xpnd_gd_zs "log Military Spending"
lab var theta_mean2 "Repression"
lab var v2dlencmps "Public Goods"

sutex  lnchinadebt_gdp2 totalproject2 v2x_execorr growth v2x_polyarchy democracy lnrgdpnapc  ny_gdp_totl_rt_zs  max_civilwar2 lndebt v2dlencmps lnms_mil_xpnd_gd_zs theta_mean2 if year>=2000, labels minmax



****************************************************************
*** IV Stuff

**Full model
  cmp (fail=lnchinadebt_gdp2  $cntl fyrs f2 f3) ///
 (lnchinadebt_gdp2=ivch7 prch4 lnres_chn $cntl  i.ccode i.year)  ///
, ind($cmp_probit $cmp_cont) tech(dfp nr)  diff robust
estimates store iv3

  cmp (fail=totalproject2  $cntl fyrs f2 f3) ///
 (totalproject2=ivch8 prch5 lnres_chn $cntl  i.ccode i.year)  ///
, ind($cmp_probit $cmp_cont) tech(dfp nr)  diff robust
estimates store iv4

lab var ivch9 "Reserve * Pr(Loan)"
lab var ivch8 "Reserve * Pr(Project)"
lab var lnres_chn "log Curreny Reserves"
lab var prch5 "Pr(New Project)"
lab var prch6 "Pr(Loan)"


estout iv3 iv4 , equations(2:2) cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) starlevels(* 0.05 ) ///
stats( ll N, fmt(%9.2f %9.0f) labels("LL" ))  style(tex) ///
legend label collabels(none) varlabels(_cons Constant) ///
order( ivch7 prch4 lnres_chn ivch8 prch5  v2x_polyarchy  ) drop(*ccode *year) ///
mlabels ("Model 1" "Model 2" "Model 3" "Model 4" "Model 5" ) ///
title("") 

**IV diagnostic stats from this
ivreg2 fail $cntl fyrs f2 f3  ///
(lnchinadebt_gdp2  = ivch7 prch4 lnres_chn i.ccode i.year  ), gmm2s first 

***********************
**Balance regressions

cap drop quant_ivch1
  xtile quant_ivch1  =  ivch7  if year >=2000, nq(5) 

stcox lnchinadebt_gdp2 $cntl, cluster(ccode)
estimates store c1
gen sam = e(sample)	   
  
 reg  growth i.quant_ivch1  prch4 lnres_chn i.ccode i.year if year >=2000 & sam==1, cluster(ccode)
	   estimates store m1
testparm  i.quant_ivch1  
	   
	   
 reg  v2x_polyarchy i.quant_ivch1  prch4 lnres_chn i.ccode i.year if year >=2000 & sam==1, cluster(ccode)
	   estimates store m2
testparm  i.quant_ivch1  
 
 reg  lnrgdpnapc i.quant_ivch1  prch4 lnres_chn i.ccode i.year if year >=2000 & sam==1,cluster(ccode) 
	   estimates store m3
testparm  i.quant_ivch1  

 reg  ny_gdp_totl_rt_zs  i.quant_ivch1  prch4 lnres_chn i.ccode i.year if year >=2000 & sam==1, cluster(ccode)
	   estimates store m4
testparm  i.quant_ivch1  

 reg  max_civilwar2 i.quant_ivch1  prch4 lnres_chn i.ccode i.year if year >=2000 & sam==1, cluster(ccode)
	   estimates store m5
testparm  i.quant_ivch1  

 reg  debtGDP  i.quant_ivch1  prch4 lnres_chn i.ccode i.year if year >=2000 & sam==1, cluster(ccode)
	   estimates store m6
	   testparm  i.quant_ivch1  

	   
	 lab var quant_ivch1 "Quantiles of IV"
	 
estout m1 m2 m3 m4 m5 m6, cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) starlevels(* 0.05 ) ///
stats( r2 N, fmt(%9.2f %9.0f) labels("R2"))  style(tex) ///
legend label collabels(none) varlabels(_cons Constant) ///
keep(*quant_ivch1)  ///
mlabels ("Model 1" "Model 2" "Model 3" "Model 4" "Model 5" ) ///
title("") 


	   


************
**IV and Countries that recognize Taiwan

gen taiwan = 1 if ccode == 41 | ccode ==90 | ccode ==91 | ccode ==93 | ccode ==42 | ccode ==92 | ccode ==439 | ccode ==940 | ccode ==946 ///
| ccode ==150 | ccode ==80 | ccode ==56 | ccode ==57 | ccode ==60 | ccode ==93 | ccode ==986 | ccode ==983


sum lnchinadebt_gdp2 if taiwan==1



stcox lnres_chn if  taiwan==1 & year >=2000, cluster(ccode)
stcox lnres_chn if  taiwan==1, cluster(ccode) 

stcox lnres_chn $cntl if  taiwan==1 & year >=2000, cluster(ccode)
estimates store f1


estout f1, cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) starlevels(* 0.05 ) ///
stats( ll N, fmt(%9.2f %9.0f) labels("LL" ))  style(tex) ///
legend label collabels(none) varlabels(_cons Constant) ///
keep(lnres_chn )  ///
mlabels ("Model 1" "Model 2" "Model 3" "Model 4" "Model 5" ) ///
title("") 
	   
	 
cap drop sample	 
stcox lnchinadebt_gdp2  $cntl , cluster(ccode)
gen sample = e(sample)	 

	   
	      reg    dgs10end i.quant_ivch1 lnres_chn prch4 i.ccode  if year >=2000 & sample==1 , cluster(ccode)
	   estimates store m1
	   
	   estout m1, cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) starlevels(* 0.05 ) ///
stats( r2 N, fmt(%9.2f %9.0f) labels("R2"))  style(tex) ///
legend label collabels(none) varlabels(_cons Constant) ///
keep(*quant_ivch1)  ///
mlabels ("Model 1" "Model 2" "Model 3" "Model 4" "Model 5" ) ///
title("") 
	   
	 
	 cap drop ivus
gen ivus = dgs10end*prch4
gen ivus2 = dgs10end*prch5

**Full model
  cmp (fail=lnchinadebt_gdp2  $cntl fyrs f2 f3) ///
 (lnchinadebt_gdp2= ivch7 ivus prch4 lnres_chn $cntl ivus dgs10end i.ccode i.year)  ///
, ind($cmp_probit $cmp_cont) tech(dfp nr)  diff robust
estimates store iv3

  cmp (fail=totalproject2  $cntl fyrs f2 f3) ///
 (totalproject2=ivch8  prch5 lnres_chn $cntl ivus2 dgs10end  i.ccode i.year)  ///
, ind($cmp_probit $cmp_cont) tech(dfp nr)  diff robust
estimates store iv4


lab var ivch7 "Reserve * Pr(Loan)"
lab var ivch8 "Reserve * Pr(Project)"
lab var lnres_chn "log Curreny Reserves"
lab var prch5 "Pr(New Project)"
lab var prch4 "Pr(Loan)"
lab var dgs10end "U.S. Yield"
lab var ivus "U.S. Yield * Pr(Loan)"
lab var ivus2 "U.S. Yield * Pr(New Project)"
lab var lnchinadebt_gdp2 "log China Debt"
lab var totalproject2 "New China Projects"

estout iv3 iv4 , equations(2:2) cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) starlevels(* 0.05 ) ///
stats( ll N, fmt(%9.2f %9.0f) labels("LL" ))  style(tex) ///
legend label collabels(none) varlabels(_cons Constant)  ///
order(lnchinadebt_gdp2 totalproject2 ivch7  ivch8  v2x_polyarchy  ) drop($cntl *ccode *year prch4 lnres_chn prch5 fyrs f2 f3 ) ///
mlabels ("Model 1" "Model 2" "Model 3" "Model 4" "Model 5" ) ///
title("") 
	   
	   

******************
**** Cox Diagnostics

stcox  lnchinadebt_gdp2   $cntl, cluster(ccode)
estimates store c1

estat phtest, log detail 	   
	   
	   
    estat phtest,  plot(v2x_polyar) yline(0, lcolor(black)) msym(oh) name(s2, replace) ///
title("") xtitle("Leader Duration (Days)")  ytitle("Scaled Schoenfeld Residuals Polyarchy")  ///
 graphregion(color(white)) bgcolor(white) note("") 
gr save res1, replace
 
     estat phtest, log plot(v2x_polyar) yline(0, lcolor(black)) msym(oh) name(s2, replace) ///
title("") xtitle("Leader Duration (log scale)")  ytitle("Scaled Schoenfeld Residuals Polyarchy")  ///
 graphregion(color(white)) bgcolor(white) note("")  xlabel(0 20 100 400 2000 10000) 

 gr save res2, replace
 
 graph combine res1.gph res2.gph, title("") scheme(s1manual)
 
*graph export "C:\Users\patri\Box\IMFCCP\04 Drafts\pics\poly_resids.pdf", as(pdf) replace		
	   
	   
	   
	   
     estat phtest,  plot(lnchinadebt_gdp2) yline(0, lcolor(black)) msym(oh) name(s2, replace) ///
title("") xtitle("Leader Duration (Days)")  ytitle("Scaled Schoenfeld Residuals China Loans")  ///
 graphregion(color(white)) bgcolor(white) note("") 
gr save res3, replace
 
     estat phtest, log plot(lnchinadebt_gdp2) yline(0, lcolor(black)) msym(oh) name(s2, replace) ///
title("") xtitle("Leader Duration (log scale)")  ytitle("Scaled Schoenfeld Residuals China Loans")  ///
 graphregion(color(white)) bgcolor(white) note("")  xlabel(0 20 100 400 2000 10000) 

 gr save res4, replace
 
 graph combine res3.gph res4.gph, title("") scheme(s1manual)
*graph export "C:\Users\patri\Box\IMFCCP\04 Drafts\pics\CH_resids.pdf", as(pdf) replace		
	   
	   
	   

	   
**Robust to various model choices
stcox  lnchinadebt_gdp2   $cntl , tvc(v2x_polyarchy ) cluster(ccode) texp(ln(_t)) 
estimates store r1a


stcox    lnchinadebt_gdp2    $cntl  , tvc(v2x_polyarchy) texp(ln(_t)) shared(ccode)  forceshared
estimates store r2


stcox  lnchinadebt_gdp2    $cntl2   ,  strata(democracy)
estimates store r5





estout c1 r1a r2 r5,  cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) starlevels(* 0.05 ) ///
stats( ll N, fmt(%9.2f %9.0f) labels("LL" ))  style(tex) ///
legend label collabels(none) varlabels(_cons Constant) ///
order(lnchinadebt_gdp2 v2x_polyarchy  ) ///
mlabels ("Model 1" "Model 2" "Model 3" "Model 4" "Model 5" ) ///
title("")   equations(1:1:1:1)
	   
	   
	   
	   
	   
********************************************************88
****Accountability and Transparency

gen ch_tran = count_na_scaled*lnchinadebt_gdp2

stcox lnchinadebt_gdp2 v2x_execorr count_na_scaled $cntl , cluster(ccode)
	  estimates store t0
 
	   
stcox lnchinadebt_gdp2 v2x_execorr lnch_corr count_na_scaled $cntl , cluster(ccode)
estimates store t1


stcox lnchinadebt_gdp2 v2x_execorr count_na_scaled ch_tran $cntl , cluster(ccode)
estimates store t2


stcox lnchinadebt_gdp2 count_na_scaled ch_tran $cntl , cluster(ccode)




estout t0 t1 t2,  cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) starlevels(* 0.05 ) ///
stats( ll N, fmt(%9.2f %9.0f) labels("LL" ))  style(tex) ///
legend label collabels(none) varlabels(_cons Constant) ///
order( lnchinadebt_gdp2 v2x_execorr lnch_corr  count_na_scaled  ch_tran) ///
mlabels ("Model 1" "Model 2" "Model 3" "Model 4" "Model 5" ) ///
title("")   


***********************************************
*Leader Experience (A14)

use "C:\Users\Patrick Shea\Box\IMFCCP\02 Analysis\ChinaLead_JCR.dta", clear 

xtset leadid2 year
drop if oecd==1

**Time Set Data 
stset enddate2 , id(leadid2) failure(fail==1) origin(time eindate) enter(time startdate2)

**Controls
global cntl growth v2x_polyarchy lnrgdpnapc  ny_gdp_totl_rt_zs  max_civilwar2 lndebt 

*Without democracy
global cntl2 growth  lnrgdpnapc  ny_gdp_totl_rt_zs  max_civilwar2 lndebt 



stcox lnchinadebt_gdp2 $cntl  age, cluster(ccode)

estimates store c1

stcox c.lnchinadebt_gdp2##c.age $cntl  , cluster(ccode)
estimates store c2


stcox c.totalproject2##c.age $cntl , cluster(ccode) nohr


stcox lnchinadebt_gdp2 $cntl gamma, cluster(ccode)
estimates store c3


stcox c.lnchinadebt_gdp2##c.gamma $cntl, cluster(ccode)
estimates store c4

stcox c.totalproject2##c.gamma $cntl , cluster(ccode) nohr

lab var age "Leader Age"

estout c1 c2 c3 c4,  cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) starlevels(* 0.05 ) ///
stats( ll N, fmt(%9.2f %9.0f) labels("LL" ))  style(tex) ///
legend label collabels(none) varlabels(_cons Constant) ///
order( lnchinadebt_gdp2 age gamma lnchinadebt_gdp2#c.age lnchinadebt_gdp2#c.gamma v2x_execorr  ) ///
mlabels ("Model 1" "Model 2" "Model 3" "Model 4" "Model 5" ) ///
title("") 

**********************************************
*****Interaction Graphs A15
*** offshore accounts

use "C:\Users\Patrick Shea\Box\IMFCCP\02 Analysis\ChinaLead_JCR.dta", clear 
xtset leadid2 year
drop if oecd==1

**Time Set Data 
stset enddate2 , id(leadid2) failure(fail==1) origin(time eindate) enter(time startdate2)

**Controls
global cntl growth v2x_polyarchy lnrgdpnapc  ny_gdp_totl_rt_zs  max_civilwar2 lndebt 

*Without democracy
global cntl2 growth  lnrgdpnapc  ny_gdp_totl_rt_zs  max_civilwar2 lndebt 

reg F.taxpertotal    c.lnchinadebt_gdp2##c.v2x_execorr  $cntl  i.ccode

set scheme s1mono
margins, dydx(lnchinadebt_gdp2)  at(v2x_execorr = (0(.05)1)) 
marginsplot, recast(line) recastci(rarea) yline(0, lwidth(thin) lpattern(dash)) graphregion(color(white)) ///
title("Model 2") ytitle("") ///
name(o1, replace) xlabel(0 (.2) 1)

xtreg F.taxpertotal  c.lnchinadebt_gdp2 c.v2x_execorr  $cntl  i.ccode


xtreg F.taxpertotal  c.lnchinadebt_gdp2##c.v2x_execorr  $cntl  i.ccode

set scheme s1mono
margins, dydx(v2x_execorr )  at(lnchinadebt_gdp2= (0(.25)4)) 
marginsplot, recast(line) recastci(rarea) yline(0, lwidth(thin) lpattern(dash)) graphregion(color(white)) ///
title("Model 2") ytitle("") ///
name(o1a, replace) xlabel(0 (1) 4) xtitle(ln (China Loans))

**graph export "C:\Users\Patrick Shea\Box\IMFCCP\04 Drafts\pics\off1.png", as(png) name("o1a")


reg F.d.taxpertotal taxpertotal  c.lnchinadebt_gdp2##c.v2x_execorr  $cntl  i.ccode 

margins, dydx(lnchinadebt_gdp2)  at(v2x_execorr = (0(.05)1)) asbalanced
marginsplot, recast(line) recastci(rarea) yline(0, lwidth(thin) lpattern(dash)) graphregion(color(white)) ///
title("Model 3") ytitle("") ///
name(o2, replace) xlabel(0 (.2) 1)

set scheme s1mono
margins, dydx(v2x_execorr )  at(lnchinadebt_gdp2= (0(.25)4) )  
marginsplot, recast(line) recastci(rarea) yline(0, lwidth(thin) lpattern(dash)) graphregion(color(white)) ///
title("Model 3") ytitle("") ///
name(o2a, replace) xlabel(0 (1) 4) xtitle(ln (China Loans))


reg F.d.taxpertotal taxpertotal   lnchinadebt_gdp2 v2x_execorr lnch_corr  $cntl  i.ccode 
estimates store o2

reg F.d.taxpertotal taxpertotal  c.lnchinadebt_gdp2##c.v2x_execorr  $cntl  i.ccode 



margins, dydx(lnchinadebt_gdp2)  at(v2x_execorr = (0(.05)1)) asbalanced force
marginsplot, recast(line) recastci(rarea) yline(0, lwidth(thin) lpattern(dash)) graphregion(color(white)) ///
title("Model 3") ytitle("") ///
name(o2, replace) xlabel(0 (.2) 1)

set scheme s1mono
margins, dydx(v2x_execorr )  at(lnchinadebt_gdp2= (0(.25)4) )  
marginsplot, recast(line) recastci(rarea) yline(0, lwidth(thin) lpattern(dash)) graphregion(color(white)) ///
title("Model 3") ytitle("") ///
name(o2a, replace) xlabel(0 (1) 4) xtitle(ln (China Loans))


**graph export "C:\Users\Patrick Sheack Shea\Box\IMFCCP\04 Drafts\pics\off2.png", as(png) name("o2a")



reg F.Anderspertotal   lnchinadebt_gdp2 v2x_execorr lnch_corr $cntl  i.ccode
estimates store o3

reg F.Anderspertotal  c.lnchinadebt_gdp2##c.v2x_execorr $cntl i.ccode 

margins, dydx(lnchinadebt_gdp2)  at(v2x_execorr = (0(.05)1)) 
marginsplot, recast(line) recastci(rarea) yline(0, lwidth(thin) lpattern(dash)) graphregion(color(white)) ///
title("Model 4") ytitle("") ///
name(o3, replace) xlabel(0 (.2) 1)

set scheme s1mono
margins, dydx(v2x_execorr )  at(lnchinadebt_gdp2= (0(.25)4)) 
marginsplot, recast(line) recastci(rarea) yline(0, lwidth(thin) lpattern(dash)) graphregion(color(white)) ///
title("Model 4") ytitle("") ///
name(o3a, replace) xlabel(0 (1) 4) xtitle(ln (China Loans))


**graph export "C:\Users\Patrick Sheack Shea\Box\IMFCCP\04 Drafts\pics\off3.png", as(png) name("o3a")


reg F.d.Anderspertotal  Anderspertotal lnchinadebt_gdp2 v2x_execorr lnch_corr  $cntl  i.ccode
estimates store o4

reg F.d.Anderspertotal  Anderspertotal  c.lnchinadebt_gdp2##c.v2x_execorr  $cntl  i.ccode

margins, dydx(lnchinadebt_gdp2)  at(v2x_execorr = (0(.05)1)) 
marginsplot, recast(line) recastci(rarea) yline(0, lwidth(thin) lpattern(dash)) graphregion(color(white)) ///
title("Model 5") ytitle("") ///
name(o4, replace) xlabel(0 (.2) 1)

set scheme s1mono
margins, dydx(v2x_execorr )  at(lnchinadebt_gdp2= (0(.25)4)) 
marginsplot, recast(line) recastci(rarea) yline(0, lwidth(thin) lpattern(dash)) graphregion(color(white)) ///
title("Model 5") ytitle("") ///
name(o4a, replace) xlabel(0 (1) 4) xtitle(ln (China Loans))

graph combine o1a o2a o3a o4a, ycommon title("") scheme(s1manual) l1("Marginal Effects of Corruption on Offshore Accounts")

**graph export "C:\Users\Patrick Shea\Box\IMFCCP\04 Drafts\pics\offall.png", as(png) name("Graph") replace

graph combine o1 o2 o3 o4, ycommon title("") scheme(s1manual) l1("Marginal Effects of China Debt on Offshore Accounts")

**graph export "C:\Users\Patrick Shea\Box\IMFCCP\04 Drafts\pics\offall2.png", as(png) name("Graph") replace


tab ccode, g(cc_)

gen Ftaxpertotal= F.taxpertotal

interflex Ftaxpertotal  lnchinadebt_gdp2 v2x_execorr  $cntl cc_*,   title("") ylabel("Offshore") xlabel("Leader Corruption") dlabel("China Debt") 



**graph export "C:\Users\Patrick Shea\Box\IMFCCP\04 Drafts\pics\interflex1.png", as(png) name("Graph") replace


interflex Ftaxpertotal v2x_execorr lnchinadebt_gdp2   $cntl cc_*, type(kernel) bw(1.2)    title("") ylabel("Offshore") dlabel("Leader Corruption") xlabel("China Debt") 

**graph export "C:\Users\Patrick Shea\Box\IMFCCP\04 Drafts\pics\interflex2.png", as(png) name("Graph") replace




interflex Ftaxpertotal lnchinadebt_gdp2 v2x_execorr   $cntl cc_*, nbin(3)    title("") ylabel("Offshore") dlabel("Leader Corruption") xlabel("China Debt") 

**graph export "C:\Users\Patrick Shea\Box\IMFCCP\04 Drafts\pics\interflex3.png", as(png) name("Graph") replace

interflex Ftaxpertotal  lnchinadebt_gdp2 v2x_execorr  $cntl cc_*,  type(kernel) bw(.3) title("") ylabel("Offshore") xlabel("Leader Corruption") dlabel("China Debt") 

**graph export "C:\Users\Patrick Shea\Box\IMFCCP\04 Drafts\pics\interflex4.png", as(png) name("Graph") replace





******************
**A9 Different Project Sectors

use "C:\Users\Patrick Shea\Box\IMFCCP\02 Analysis\ChinaLead_JCR.dta", clear 

xtset leadid2 year
drop if oecd==1

**Time Set Data 
stset enddate2 , id(leadid2) failure(fail==1) origin(time eindate) enter(time startdate2)

**Controls
global cntl growth v2x_polyarchy lnrgdpnapc  ny_gdp_totl_rt_zs  max_civilwar2 lndebt 

*Without democracy
global cntl2 growth  lnrgdpnapc  ny_gdp_totl_rt_zs  max_civilwar2 lndebt 

cap drop _m


global cntl growth v2x_polyarchy lnrgdpnapc  ny_gdp_totl_rt_zs  max_civilwar2 lndebt 


gen NonC = totalproject2 - totalC2proj
gen NonCdis = totaldisp2- totaldispC2
gen lnNonCdis2 = lntotaldisp2-lntotaldispC2

replace NonCdis = 0 if NonCdis==. & year >=2000

stcox NonC totalC2proj v2x_execorr $cntl, cluster(ccode)
estimates store c1 

lincom NonC - totalC2proj 


sum NonC totalC2proj
sum NonCdis totaldispC2

histogram NonC, frequency kdensity ///
name(hist1, replace)
histogram totalC2proj, frequency kdensity ///
name(hist2, replace)
graph combine hist1 hist2, ycommon

*graph export "C:\Users\Patrick Shea\Box\IMFCCP\04 Drafts\pics\dist_projects.png", as(png) name("Graph")

stcox c.NonC##c.v2x_execorr c.totalC2proj##c.v2x_execorr  $cntl, cluster(ccode)

stcox  c.totalC2proj##c.v2x_execorr  $cntl, cluster(ccode)
estimates store c2 

stcox c.NonC##c.v2x_execorr   $cntl, cluster(ccode)
estimates store c3 

lab var totalC2proj  "Corruptible Projects"
lab var NonC   "Non Corruptible Projects"
lab var v2x_execorr "Leader Corruption"
 
estout c1 c2 c3,  cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) starlevels(* 0.05 ) ///
stats( ll N, fmt(%9.2f %9.0f) labels("LL" ))  style(tex) ///
legend label collabels(none) varlabels(_cons Constant) ///
order(totalC2proj NonC v2x_execorr ) ///
mlabels ("Model 1" "Model 2" "Model 3" "Model 4" "Model 5" ) ///
title("")   



***************************************************************
**Clientelism (A16)

use "C:\Users\Patrick Shea\Box\IMFCCP\02 Analysis\ChinaLead_JCR.dta", clear 
xtset leadid2 year
drop if oecd==1

**Time Set Data 
stset enddate2 , id(leadid2) failure(fail==1) origin(time eindate) enter(time startdate2)

**Controls
global cntl growth v2x_polyarchy lnrgdpnapc  ny_gdp_totl_rt_zs  max_civilwar2 lndebt 

*Without democracy
global cntl2 growth  lnrgdpnapc  ny_gdp_totl_rt_zs  max_civilwar2 lndebt 




stcox c.lnchinadebt_gdp2 v2xnp_client $cntl  , cluster(ccode)
estimates store c1

stcox c.lnchinadebt_gdp2##c.v2xnp_client $cntl  , cluster(ccode)
estimates store c2

lab var v2xnp_client "Clientelism"


estout c1 c2,  cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) starlevels(* 0.05 ) ///
stats( ll N, fmt(%9.2f %9.0f) labels("LL" ))  style(tex) ///
legend label collabels(none) varlabels(_cons Constant) ///
order( lnchinadebt_gdp2 v2xnp_client lnchinadebt_gdp2#v2xnp_client   ) ///
mlabels ("Model 1" "Model 2" "Model 3" "Model 4" "Model 5" ) ///
title("") 





*******************************
**A6 High Income and Upper-Middle Income Countries


stcox c.lnchinadebt_gdp2##midhigh $cntl, cluster(ccode)

stcox c.lnchinadebt_gdp2##midhigh2 $cntl, cluster(ccode)


stcox lnchinadebt_gdp2 $cntl if midhigh==1, cluster(ccode)

stcox lnchinadebt_gdp2 $cntl  if midhigh==0, cluster(ccode)


stcox lnchinadebt_gdp2 v2x_execorr lnch_corr $cntl if midhigh==1, cluster(ccode)

stcox lnchinadebt_gdp2 v2x_execorr lnch_corr $cntl  if midhigh==0, cluster(ccode)




********************************************
*Elections

use "C:\Users\Patrick Shea\Box\IMFCCP\02 Analysis\ChinaLead_JCR.dta", clear 

xtset leadid2 year
drop if oecd==1

**Time Set Data 
stset enddate2 , id(leadid2) failure(fail==1) origin(time eindate) enter(time startdate2)

**Controls
global cntl growth v2x_polyarchy lnrgdpnapc  ny_gdp_totl_rt_zs  max_civilwar2 lndebt 

*Without democracy
global cntl2 growth  lnrgdpnapc  ny_gdp_totl_rt_zs  max_civilwar2 lndebt 

cap drop _m

 merge m:1 ccode year using "C:\Users\Patrick Shea\Box\IMFCCP\02 Analysis\neldaelections.dta"
 
drop if _m==2
drop _m

xtset leadid2 year

gen maxelec2 = maxelec
replace maxelec2 = 0 if maxelec==. 

gen maxelec_leadup = maxelec2
replace maxelec_leadup  = 1 if f.maxelec2==1 & maxelec2==0

gen maxelec_leadup2 = maxelec2
replace maxelec_leadup2  = 1 if f.maxelec2==1 & maxelec2==0
replace maxelec_leadup2  = 1 if f2.maxelec2==1 & maxelec2==0

lab var maxelec2 "Imcumbent Election"
lab var maxelec_leadup "Election Leadup"
lab var maxelec_leadup2 "Election Leadup 2"


stcox c.lnchinadebt_gdp2 maxelec2 $cntl, cluster(ccode)
estimates store e1

stcox c.lnchinadebt_gdp2##c.maxelec2 $cntl, cluster(ccode)
estimates store e2

stcox c.lnchinadebt_gdp2##c.maxelec_leadup $cntl, cluster(ccode)
estimates store e3

stcox c.lnchinadebt_gdp2##c.maxelec_leadup2 $cntl, cluster(ccode)
estimates store e4

estout e1 e2 e3 e4,  cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) starlevels(* 0.05 ) ///
stats( ll N, fmt(%9.2f %9.0f) labels("LL" ))  style(tex) ///
legend label collabels(none) varlabels(_cons Constant) ///
order( lnchinadebt_gdp2 maxelec2  lnchinadebt_gdp2#c.maxelec2  maxelec_leadup lnchinadebt_gdp2#c.maxelec_leadup  maxelec_leadup2  lnchinadebt_gdp2#c.maxelec_leadup2   ) ///
mlabels ("Model 1" "Model 2" "Model 3" "Model 4" "Model 5" ) ///
title("") 

reg  lnchinadebt_gdp2  maxelec_leadup2 i.ccode $cntl
reg  lnchinadebt_gdp2  maxelec_leadup i.ccode $cntl
reg  lnchinadebt_gdp2  maxelec2  i.ccode $cntl

reg f.lnchinadebt_gdp2 lnchinadebt_gdp2  maxelec_leadup2 i.ccode $cntl
estimates store e3

reg f.lnchinadebt_gdp2 lnchinadebt_gdp2  maxelec_leadup i.ccode $cntl
estimates store e2

reg f.lnchinadebt_gdp2 lnchinadebt_gdp2  maxelec2  i.ccode $cntl
estimates store e1

reg f.totalproject2 totalproject2  maxelec_leadup2 i.ccode $cntl
estimates store e6

reg f.totalproject2 totalproject2  maxelec_leadup i.ccode $cntl
estimates store e5

reg f.totalproject2 totalproject2  maxelec2  i.ccode $cntl
estimates store e4

estout e1 e2 e3 e4 e5 e6, cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) starlevels(* 0.05 ) ///
stats( ll N, fmt(%9.2f %9.0f) labels("LL" ))  style(tex) ///
legend label collabels(none) varlabels(_cons Constant) ///
keep(  maxelec2    maxelec_leadup   maxelec_leadup2  ) ///
mlabels ("Model 1" "Model 2" "Model 3" "Model 4" "Model 5" ) ///
title("") 

****************************************************************
**WB 


*WB aid
stcox lnwb  lnchinadebt_gdp2 v2x_execorr   $cntl  ,  cluster(ccode)
estimates store a0

stcox lnwb  lnchinadebt_gdp2 v2x_execorr wbcorr   $cntl  ,  cluster(ccode)
estimates store a0a

estout a0 a0a ,  cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) starlevels(* 0.05 ) ///
stats( ll N, fmt(%9.2f %9.0f) labels("LL" ))  style(tex) ///
legend label collabels(none) varlabels(_cons Constant) ///
order(lnchinadebt_gdp2 lnwb v2x_execorr wbcorr     ) ///
mlabels ("Model 1" "Model 2"  ) ///
title("") 




**********************Regime Type Stuff********************************

global cntl2 growth  lnrgdpnapc  ny_gdp_totl_rt_zs  max_civilwar2 lndebt 

gen ch_poly = v2x_polyarchy*lnchinadebt_gdp2
gen chproj_poly = v2x_polyarchy*totalproject2

gen ch_dem = democracy*lnchinadebt_gdp2
gen chproj_dem = democracy*totalproject2


stcox lnchinadebt_gdp2 v2x_polyarchy ch_poly $cntl2 , cluster(ccode)
estimates store r1

estat phtest, log detail

stcox lnchinadebt_gdp2 v2x_polyarchy ch_poly $cntl2 , tvc(v2x_polyarchy) cluster(ccode) texp(ln(_t))
estimates store r1a

stcox lnchinadebt_gdp2 v2x_polyarchy ch_poly $cntl2 , tvc(v2x_polyarchy ch_poly ) cluster(ccode) texp(ln(_t))

stcox lnchinadebt_gdp2 democracy ch_dem  $cntl2 ,  cluster(ccode) 
estimates store r2



estat phtest, log detail




stcox lnchinadebt_gdp2 democracy ch_dem  $cntl2 , tvc(democracy) cluster(ccode) texp(ln(_t))
estimates store r2a



stcox lnchinadebt_gdp2  $cntl2 if democracy==1,  cluster(ccode) 
estimates store d1

stcox lnchinadebt_gdp2  $cntl2 if democracy==0,  cluster(ccode) 
estimates store d0

stcox lnchinadebt_gdp2 v2x_execorr lnch_corr  $cntl2 if democracy==1,  cluster(ccode) 
estimates store d1x

stcox lnchinadebt_gdp2 v2x_execorr lnch_corr $cntl2 if democracy==0,  cluster(ccode) 
estimates store d0x

stcox c.lnchinadebt_gdp2##c.v2x_execorr##democracy   $cntl2,  cluster(ccode) 
stcox c.lnchinadebt_gdp2##c.v2x_execorr##c.v2x_polyarchy  $cntl2,  cluster(ccode) 

estout  d1 d0 d1x d0x , cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) starlevels(* 0.05 ) ///
stats( ll N, fmt(%9.2f %9.0f) labels("LL" ))  style(tex) ///
legend label collabels(none) varlabels(_cons Constant) ///
order(lnchinadebt_gdp2 totalproject2 v2x_execorr lnch_corr lnch_corr2 growth lnch_gr   v2dlencmps lnch_pg  lnms_mil_xpnd_gd_zs lnch_mil    theta_mean2 lnch_repr v2x_polyarchy)  ///
mlabels ("Model 1" "Model 2" "Model 3" "Model 4" "Model 5" ) ///
title("") 

*interactions setup

gen xxx2 = ln(_t)*lnchinadebt_gdp2*democracy
gen lnt = ln(_t)

gen ct2 = ln(_t)*lnchinadebt_gdp2
gen dt2 = ln(_t)*democracy


stcox lnchinadebt_gdp2 democracy ct2 dt2 lnt ch_dem xxx2  $cntl2 , cluster(ccode) iterate(7)


stcox totalproject2 v2x_polyarchy chproj_poly $cntl2 , cluster(ccode)

stcox totalproject2 democracy chproj_dem $cntl2 , cluster(ccode)





gen tch = lnt*lnchinadebt_gdp2 

gen chDem2 = lnchinadebt_gdp2*democracy
gen tchDem2 = lnchinadebt_gdp2*lnt*democracy 
gen tDem = lnt*democracy 
gen chPol2 = lnchinadebt_gdp2*v2x_polyarchy

gen ivdem = ivch7*v2x_polyarchy
gen ivcorr = ivch7*v2x_execorr

gen ivdem2 = ivch7*democracy
gen prdem2 = prch4*democracy
gen resdem2 = lnres_chn*democracy

gen prdem = prch4*v2x_polyarchy
gen resdem = lnres_chn*v2x_polyarchy

**# Bookmark #7
gen prcor = prch4*v2x_execorr
gen rescor = lnres_chn*v2x_execorr


  cmp (fail=lnchinadebt_gdp2  v2x_polyarchy ch_poly  $cntl2 fyrs f2 f3) ///
 (lnchinadebt_gdp2=ivch7 prch4 lnres_chn  $cntl  i.ccode i.year)  ///
, ind($cmp_probit $cmp_cont) tech(dfp nr)  diff robust
estimates store r3

  cmp (fail=lnchinadebt_gdp2  v2x_polyarchy ch_poly  $cntl2 fyrs f2 f3) ///
 (lnchinadebt_gdp2=ivch7 prch4 lnres_chn  $cntl  i.ccode i.year)  ///
  (ch_poly= ivdem ivch7 prch4 lnres_chn  $cntl  i.ccode i.year)  ///
, ind($cmp_probit $cmp_cont $cmp_cont) tech(dfp nr)  diff robust

  cmp (fail=lnchinadebt_gdp2  democracy ch_dem  $cntl2 fyrs f2 f3) ///
 (lnchinadebt_gdp2=ivch7 prch4 lnres_chn democracy $cntl2  i.ccode i.year)  ///
  (ch_dem= ivdem2 ivch7 prch4 lnres_chn democracy prdem2 resdem2 $cntl2  i.ccode i.year)  ///
, ind($cmp_probit $cmp_cont $cmp_cont) tech(dfp nr)  diff robust



estimates store r3

  cmp (fail=lnchinadebt_gdp2 v2x_execorr lnch_corr  $cntl fyrs f2 f3) ///
 (lnchinadebt_gdp2=ivch7 prch4 lnres_chn $cntl  i.ccode i.year)  ///
 (lnch_corr= ivcorr ivch7 prch4 lnres_chn $cntl  i.ccode i.year)  /// 
, ind($cmp_probit $cmp_cont $cmp_cont) tech(dfp nr)  diff robust
estimates store r3a

  cmp (fail=lnchinadebt_gdp2 v2x_execorr lnch_corr  $cntl fyrs f2 f3) ///
 (lnchinadebt_gdp2=ivch7 prch4 lnres_chn $cntl  i.ccode i.year)  ///
 (lnch_corr= ivcorr ivch7 prch4 lnres_chn rescor prcor $cntl  i.ccode i.year)  /// 
, ind($cmp_probit $cmp_cont $cmp_cont) tech(dfp nr)  diff robust



cap drop xb se pfail Z


probit fail lnchinadebt_gdp2 democracy $cntl lnt, cl(ccode)

		predict double xb, xb
			
			lab var xb "Linear index of wfail2 equation with W dummy"
		
		predict double se, stdp
		
			lab var se "Standard Error of wfail2 equation with W dummy"
		
		predict double pfail, p
			
			lab var pfail "Probability of Failure from wfail2 Equation with W dummy"
		
		gen Z = xb/se
			
			lab var Z "Linear index of wfail2 equation adjusted by standard error with W dummy, Maddala style"
			


  cmp (fail=lnchinadebt_gdp2  lnt $cntl ) ///
 (lnchinadebt_gdp2= Z  $cntl i.ccode i.year)  ///
, ind($cmp_probit $cmp_cont) tech(dfp nr)  diff robust
estimates store r4


cap drop xb se pfail Z

probit fail lnchinadebt_gdp2 lnt ch_dem tchDem2 tch tDem democracy $cntl2 , cl(ccode)


		predict double xb, xb
			
			lab var xb "Linear index of wfail2 equation with W dummy"
		
		predict double se, stdp
		
			lab var se "Standard Error of wfail2 equation with W dummy"
		
		predict double pfail, p
			
			lab var pfail "Probability of Failure from wfail2 Equation with W dummy"
		
		gen Z = xb/se


  cmp (fail=lnchinadebt_gdp2 democracy lnt ch_dem  tDem tch tchDem2   $cntl2 ) ///
 (lnchinadebt_gdp2= Z democracy $cntl2 i.ccode i.year )  ///
, ind($cmp_probit $cmp_cont) tech(dfp nr)  diff robust
estimates store r5


cap drop xb se pfail Z



probit fail lnchinadebt_gdp2 lnt v2x_execorr lnch_corr $cntl , cl(ccode)


		predict double xb, xb
			
			lab var xb "Linear index of wfail2 equation with W dummy"
		
		predict double se, stdp
		
			lab var se "Standard Error of wfail2 equation with W dummy"
		
		predict double pfail, p
			
			lab var pfail "Probability of Failure from wfail2 Equation with W dummy"
		
		gen Z = xb/se


  cmp (fail=lnchinadebt_gdp2 lnchinadebt_gdp2 v2x_execorr lnch_corr  lnt   $cntl ) ///
 (lnchinadebt_gdp2= Z $cntl i.ccode i.year)  ///
, ind($cmp_probit $cmp_cont) tech(dfp nr)  diff robust
estimates store r6

lab var lnchinadebt_gdp2 "log China Debt"
lab var totalproject2 "New China Projects"


lab var v2x_execorr "Executive Corruption"

lab var v2x_polyarchy "Polyarchy"
lab var lnrgdpe "Log GDP"
lab var growth "Growth"

 
 
  lab var lnrgdpnapc "Log GDP per cap"
  lab var ny_gdp_totl_rt_zs "Resource Rents"
  lab var max_civilwar2 "Civil Conflict"
lab var lndebt "log Debt/GDP"
lab var lnchinadebt_gdp2 "log China Debt"

lab var ivch7 "Reserves  $\times$ Pr(Loan)"
lab var prch4 "Pr(China Loan)"
lab var ivch9 "Reserves  $\times$ Pr(Loan)"
lab var prch6 "Pr(China Loan)"

lab var democracy "Democracy (Binary)"
lab var ch_poly "China Loan * Polyarchy"
lab var ch_dem "China Loan * Democracy"
lab var tDem "ln(t) * Democracy"
lab var tch  "ln(t) * China Loan"
lab var tchDem2  "ln(t) * China * Dem"


estout  r1a r2a r3 r3a r4 r5 r6 , equations(1:1:1:1:1:1:1) cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) starlevels(* 0.05 ) ///
stats( ll N, fmt(%9.2f %9.0f) labels("LL" ))  style(tex) ///
legend label collabels(none) varlabels(_cons Constant) ///
order(lnchinadebt_gdp2 v2x_polyarchy ch_poly democracy ch_dem v2x_execorr lnch_corr lnt  tDem tch tchDem2)  ///
drop(*ccode *year) ///
mlabels ("Model 1" "Model 2" "Model 3" "Model 4" "Model 5" ) ///
title("") 


**************************
*Exective v regime corruption

corr v2x_execorr v2x_corr

corr v2x_execorr v2x_corr if lnchinadebt_gdp2>0 & lnchinadebt_gdp2!=. 



tsline  v2x_execorr v2x_corr if iso3=="ZMB"
tsline  v2x_execorr v2x_corr if iso3=="ECU"
tsline  v2x_execorr v2x_corr if iso3=="LKA"
tsline  v2x_execorr v2x_corr if iso3=="PER"
tsline  v2x_execorr v2x_corr if iso3=="PAK"



reg v2x_execorr  l(1).v2x_execorr l(1)v2x_corr i.ccode
testparm l(1)v2x_corr
reg v2x_execorr  l(1/2).v2x_execorr l(1/2)v2x_corr i.ccode
testparm l(1/2)v2x_corr
reg v2x_execorr  l(1/3).v2x_execorr l(1/3)v2x_corr i.ccode
testparm l(1/3)v2x_corr
reg v2x_execorr  l(1/4).v2x_execorr l(1/4)v2x_corr i.ccode
testparm l(1/4)v2x_corr
reg v2x_execorr  l(1/5).v2x_execorr l(1/5)v2x_corr i.ccode
testparm l(1/5)v2x_corr


reg v2x_corr  l(1).v2x_corr l(1)v2x_execorr i.ccode
testparm l(1)v2x_execorr
reg v2x_corr  l(1/2).v2x_corr l(1/2)v2x_execorr i.ccode
testparm l(1/2)v2x_execorr
reg v2x_corr  l(1/3).v2x_corr l(1/3)v2x_execorr i.ccode
testparm l(1/3)v2x_execorr
reg v2x_corr  l(1/4).v2x_corr l(1/4)v2x_execorr i.ccode
testparm l(1/4)v2x_execorr
reg v2x_corr  l(1/5).v2x_corr l(1/5)v2x_execorr i.ccode
testparm l(1/5)v2x_execorr



xtreg wi_econ  l(1/2).wi_econ l(1/2)wi_security3, fe
testparm l(1/2)wi_security3
xtreg wi_econ  l(1/3).wi_econ l(1/3)wi_security3, fe
testparm l(1/3)wi_security3
xtreg wi_econ  l(1/4).wi_econ l(1/4)wi_security3, fe
testparm l(1/4)wi_security3
xtreg wi_econ  l(1/5).wi_econ l(1/5)wi_security3, fe
testparm l(1/5)wi_security3

********************************************************
***Dispersal

stcox lntotaldisp2 $cntl , cluster(ccode)
estimates store d1

gen lnchaid2 = ln(totalODAproj+1)

stcox lnchaid2  $cntl , cluster(ccode)
estimates store d2

stcox lnchaid2 $cntl if (lnchinadebt_gdp2==0 | lnchinadebt_gdp2==.), cluster(ccode)
estimates store d3

lab var lnchaid2 "China Aid"

lab var lntotaldisp2 "China Project Dispersal"


lab var lnchinadebt_gdp2 "log China Debt"
lab var totalproject2 "New China Projects"

lab var v2x_execorr "Executive Corruption"
lab var v2x_polyarchy "Polyarchy"
lab var lnrgdpe "Log GDP"
lab var growth "Growth"
lab var democracy "Democracy (Binary)"
lab var lnrgdpnapc "Log GDP per cap"
lab var ny_gdp_totl_rt_zs "Resource Rents"
lab var max_civilwar2 "Civil Conflict"
lab var lndebt "log Debt/GDP"


estout  d1 d2 d3  , cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) starlevels(* 0.05 ) ///
stats( ll N, fmt(%9.2f %9.0f) labels("LL" ))  style(tex) ///
legend label collabels(none) varlabels(_cons Constant) ///
order(lntotaldisp2 lnchaid2)  keep(lntotaldisp2 lnchaid2) ///
mlabels ("Model 1" "Model 2" "Model 3" "Model 4" "Model 5" ) ///
title("") 


******************************************************************************
**Accountability

gen r1 = v2x_rule*lnchinadebt_gdp2
gen r2 = v2juaccnt*lnchinadebt_gdp2
gen r3 = v2x_accountability*lnchinadebt_gdp2
gen r4 = v2x_veracc*lnchinadebt_gdp2
gen r5 = v2x_horacc*lnchinadebt_gdp2


stcox lnchinadebt_gdp2 v2x_rule r1 $cntl, cluster(ccode)
estimates store r1 
stcox lnchinadebt_gdp2 v2juaccnt r2 $cntl, cluster(ccode)
estimates store r2 

stcox lnchinadebt_gdp2 v2x_accountability r3 $cntl, cluster(ccode)
estimates store r3 

stcox lnchinadebt_gdp2 v2x_veracc r4 $cntl, cluster(ccode)
estimates store r4 

stcox lnchinadebt_gdp2 v2x_horacc r5 $cntl, cluster(ccode)
	estimates store r5 
 
 lab var lnchinadebt_gdp2 "log China Debt"

 lab var r1 "China Debt * Rule of Law"
 lab var r2 "China Debt * Judicial Acct"
 lab var r3 "China Debt * Acct"
 lab var r4 "China Debt * Vertical Acct"
 lab var r5 "China Debt * Horizontal Acct"

 
 
estout r1 r2 r3 r4 r5,  cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) starlevels(* 0.05 ) ///
stats( ll N, fmt(%9.2f %9.0f) labels("LL" ))  style(tex) ///
legend label collabels(none) varlabels(_cons Constant) ///
order( lnchinadebt_gdp2 v2x_rule r1 v2juaccnt r2 v2x_accountability r3 v2x_veracc r4 v2x_horacc r5 ) ///
mlabels ("Model 1" "Model 2" "Model 3" "Model 4" "Model 5" ) ///
title("")   
	   
